package cn.iocoder.yudao.module.pickbook.service.classhomework;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.pickbook.controller.admin.classhomework.vo.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.classhomework.ClassHomeworkDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pickbook.dal.mysql.classhomework.ClassHomeworkMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.pickbook.enums.ErrorCodeConstants.*;

/**
 * 班级作业 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ClassHomeworkServiceImpl implements ClassHomeworkService {

    @Resource
    private ClassHomeworkMapper classHomeworkMapper;

    @Override
    public Long createClassHomework(ClassHomeworkSaveReqVO createReqVO) {
        // 插入
        ClassHomeworkDO classHomework = BeanUtils.toBean(createReqVO, ClassHomeworkDO.class);

        // 手动生成 id
        long id = UUID.randomUUID().getLeastSignificantBits();
        classHomework.setId(id);

        classHomeworkMapper.insert(classHomework);

        // 返回
        return classHomework.getId();
    }

    @Override
    public void updateClassHomework(ClassHomeworkSaveReqVO updateReqVO) {
        // 校验存在
        validateClassHomeworkExists(updateReqVO.getId());
        // 更新
        ClassHomeworkDO updateObj = BeanUtils.toBean(updateReqVO, ClassHomeworkDO.class);
        classHomeworkMapper.updateById(updateObj);
    }

    @Override
    public void deleteClassHomework(Long id) {
        // 校验存在
        validateClassHomeworkExists(id);
        // 删除
        classHomeworkMapper.deleteById(id);
    }

    @Override
        public void deleteClassHomeworkListByIds(List<Long> ids) {
        // 删除
        classHomeworkMapper.deleteByIds(ids);
        }


    private void validateClassHomeworkExists(Long id) {
        if (classHomeworkMapper.selectById(id) == null) {
            throw exception(CLASS_HOMEWORK_NOT_EXISTS);
        }
    }

    @Override
    public ClassHomeworkDO getClassHomework(Long id) {
        return classHomeworkMapper.selectById(id);
    }

    @Override
    public PageResult<ClassHomeworkDO> getClassHomeworkPage(ClassHomeworkPageReqVO pageReqVO) {
        return classHomeworkMapper.selectPage(pageReqVO);
    }

}